package Tabs;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;

import MainMethods.FileWr;
import MainMethods.HoverImage;
import MainMethods.Screenshot;

public class PlayMusic {
	String src;
	String url;
	String songName;
	Screenshot scsh = new Screenshot();
	HoverImage hover = new HoverImage();

	// play music in Most popular section
	public void playAlbums(WebDriver driver, FileWr file, String screenDir) {
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.findElement(By.id("albums_tab")).click();
		file.writeFile("Start MUSIC Test");
		System.out.println("MUSIC");
		file.writeFile("Open Music tab - " + driver.getCurrentUrl());
		System.out.println("loading page - " + driver.getCurrentUrl());
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// add album to queue
		try {
			hover.addToQueue(driver);
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// check if message appeared
			if (hover.doesElementExist(driver, By.className("message")) == true) {
//				driver.findElement(By.xpath("//*[@id=\"feedback\"]/div[3]"))
//						.click();
				System.out.println("Add Album to Queue - PASSED");
				file.writeFile("Add Album to Queue - PASSED");
				driver.findElement(By.xpath("//*[@id=\"feedback\"]/div[3]")).click();
			}
		} catch (NoSuchElementException e) {
			System.out.println("Add Album to Queue - FAILED");
			file.writeFile("Add Album to Queue - FAILED");
			scsh.makeScreen("Add Album to Queue - FAILED", screenDir);
		}
		// Add album to collection
		try {
			hover.addToCollectionMus(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// check if message appeared
			if (hover.doesElementExist(driver, By.className("message")) == true) {
				System.out.println("Add Album to Collection - PASSED");
				file.writeFile("Add Album to Collection - PASSED");
				driver.findElement(By.xpath("//*[@id=\"feedback\"]/div[3]")).click();
			}
		} catch (NoSuchElementException e) {
			System.out.println("Add Album to Collection - FAILED");
			file.writeFile("Add Album to Collection - FAILED");
			scsh.makeScreen("Add Album to Collection - FAILED", screenDir);
		}
		// click play now button
		try {
			hover.playHover(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			src = driver.findElement(
					By.xpath("//*[@id=\"audio-player-poster\"]/img"))
					.getAttribute("src");
			if (driver
					.findElement(
							By.xpath("//*[@id=\"browse_element_wrapper\"]/div[12]/div/img"))
					.getAttribute("src").equals(src)) {
				System.out
						.println("Music played with 'Play now' button - PASSED");
				file.writeFile("Music played with 'Play now' button - PASSED");
			}
		} catch (NoSuchElementException e) {
			System.out.println("Music played with 'Play now' button - FAILED");
			file.writeFile("Music played with 'Play now' button - FAILED");
			scsh.makeScreen("Music played with 'Play now' button - FAILED",
					screenDir);
		}

		// click play now button "Plus section"
		try {
			hover.playNowPlusMus(driver);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			src = driver.findElement(
					By.xpath("//*[@id=\"audio-player-poster\"]/img"))
					.getAttribute("src");
			if (driver
					.findElement(
							By.xpath("//*[@id=\"browse_element_wrapper\"]/div[1]/div/img"))
					.getAttribute("src").equals(src)) {
				System.out
						.println("Music played with 'Play now' button in 'Plus' section - PASSED");
				file.writeFile("Music played with 'Play now' button in 'Plus' section - PASSED");
			}
		} catch (NoSuchElementException e) {
			System.out
					.println("Music played with 'Play now' button in 'Plus' section - FAILED");
			file.writeFile("Music played with 'Play now' button in 'Plus' section - FAILED");
			scsh.makeScreen(
					"Music played with 'Play now' button in 'Plus' section - FAILED",
					screenDir);
		}

		// enter collection
		try {
			driver.findElement(
					By.xpath("//*[@id=\"sidenav-your-stuff\"]/ul/li[1]/a"))
					.click();
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			if (hover.doesElementExist(driver,
					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]")) == true) {
				System.out.println("Open Collection - PASSED");
				file.writeFile("Open Collection - PASSED");
			} else {
				System.out.println("Open Collection - FAILED");
				file.writeFile("Open Collection - FAILED");
				scsh.makeScreen("Open Collection - FAILED", screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out.println("Open Collection - FAILED");
			file.writeFile("Open Collection - FAILED");
			scsh.makeScreen("Open Collection Exception e - FAILED", screenDir);
		}
		// play music from collection
		hover.fromColl(driver);
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println("Music played from collection");

		// enter queue and play music from queue
		try {
			driver.findElement(By.xpath("//*[@id=\"queue-link\"]")).click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			if (hover.doesElementExist(driver,
					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]")) == true) {
				System.out.println("Open QUEUE - PASSED");
				file.writeFile("Open QUEUE - PASSED");
			} else {
				System.out.println("Open QUEUE - FAILED");
				file.writeFile("Open QUEUE - FAILED");
				scsh.makeScreen("Open QUEUE - FAILED", screenDir);
			}
			Actions hovq = new Actions(driver);
			Actions qBtn = hovq
					.moveToElement(driver.findElement(By
							.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[1]/div[1]")));
			qBtn.perform();
			driver.findElement(
					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[1]/div[1]/div"))
					.click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			src = (driver
					.findElement(
							By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[1]/div[2]/a"))
					.getText()
					+ " - " + driver
					.findElement(
							By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[1]/div[3]/a"))
					.getText());
			songName = driver.findElement(
					By.xpath("//*[@id=\"audio-player-title\"]")).getText();
			System.out.println(src);
			System.out.println(songName);
			if (src.equals(songName) == true) {
				System.out.println("Music played from queuee - PASSED");
				file.writeFile("Music played from queue - PASSED");
			} else {
				System.out.println("Music played from queue - FAILED");
				file.writeFile("Music played from queue - FAILED");
				scsh.makeScreen("Music played from queue - FAILED", screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out.println("Music played from queue Exception e - FAILED");
			scsh.makeScreen("queue - FAILED exception", screenDir);
		}
		// play music from because section on music tab
		try {
			driver.findElement(By.id("albums_tab")).click();
			hover.becauseSect(driver);
			src = driver.findElement(
					By.xpath("//*[@id=\"audio-player-poster\"]/img"))
					.getAttribute("src");
			if (driver
					.findElement(
							By.xpath("//*[@id=\"browse_wrapper\"]/div[1]/div[9]/div/img"))
					.getAttribute("src").equals(src)) {
				System.out
						.println("Music played from 'Because' section by 'Play now' - PASSED");
				file.writeFile("Music played from 'Because' section by 'Play now' - PASSED");
			} else {
				System.out
						.println("Music played from 'Because' section by 'Play now' - FAILED");
				file.writeFile("Music played from 'Because' section by 'Play now' - FAILED");
			}
		} catch (NoSuchElementException e) {
			System.out
					.println("'Because' section by 'Play now' Exception e - FAILED");
			file.writeFile("'Because' section by 'Play now' Exception e - FAILED");
			scsh.makeScreen(
					"Because' section by 'Play now' Exception e FAILED",
					screenDir);
		}
		// click "More info" (plus button)
		try {
			hover.moreInfoMus(driver);
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (hover.doesElementExist(driver,
					By.xpath("//*[@id=\"music_list_container\"]/div[1]")) == true) {
				System.out.println("'More info' page opened");
				file.writeFile("'More info' page opened");
			}
		} catch (NoSuchElementException e) {
			System.out.println("'More info' page - FAILED");
			file.writeFile("'More info' page - FAILED");
			scsh.makeScreen("'More info' page - FAILED", screenDir);
		}
		// play music in More info page
		try {
			Actions hov = new Actions(driver);
			Actions hoverBtn = hov.moveToElement(driver.findElement(By
					.xpath("//*[@id=\"music_list_container\"]/div[1]/div")));
			hoverBtn.perform();
			driver.findElement(
					By.xpath("//*[@id=\"music_list_container\"]/div[1]/div/div[1]/div"))
					.click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			src = (driver
					.findElement(
							By.xpath("//*[@id=\"music_list_container\"]/div[1]/div/div[2]/span"))
					.getText()
					+ " - " + driver
					.findElement(
							By.xpath("//*[@id=\"music_list_container\"]/div[1]/div/div[3]/span"))
					.getText());
			songName = driver.findElement(
					By.xpath("//*[@id=\"audio-player-title\"]")).getText();
			System.out.println(src);
			System.out.println(songName);
			if (src.equals(songName) == true) {
				System.out
						.println("Music played from 'More info' page - PASSED");
				file.writeFile("Music played from 'More info' page - PASSED");
			} else {
				System.out
						.println("Music played from 'More info' page - FAILED");
				file.writeFile("Music played from 'More info' page - FAILED");
				scsh.makeScreen("Music played from 'More info' page - FAILED",
						screenDir);
			}
		} catch (NoSuchElementException e) {
			System.out.println("Music played from 'More info' page - FAILED");
			file.writeFile("Music played from 'More info' page - FAILED");
			scsh.makeScreen("Music played from 'More info' page 2 - FAILED",
					screenDir);
		}
	}
}